package com.aaa.service.impl;

import com.aaa.dao.RoleDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Role;
import com.aaa.entity.User;
import com.aaa.service.RoleService;
import com.aaa.vo.Result;
import com.aaa.vo.RoleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public Result<List<Role>> getAll() {
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Role> roles = roleDao.selectList(wrapper);
        return new Result<>(2000,"查询角色",roles);
    }

    @Override
    public Result<Map<String,Object>> findByUserId(Long userId) {
        List<Role> userRoles=roleDao.selectByUserId(userId);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("status",0);
        List<Role> roles = roleDao.selectList(wrapper);

        Map<String,Object> map=new HashMap<>();
        map.put("roles",roles);
        map.put("userRoles",userRoles);
        return new Result<>(2000,"查询用户角色",map);
    }

    @Transactional
    @Override
    public Result<String> confirmRole(Long userId, Long[] roleIds) {
        roleDao.deleteByUserId(userId);
        //批量添加
        roleDao.batchInsert(userId,roleIds);
        return new Result<>(2000,"分配角色成功");
    }

    @Override
    public Result<IPage<Role>> getByPage(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        IPage<Role> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if(StringUtils.hasText(roleVo.getRoleCode())){
            wrapper.eq("role_code",roleVo.getRoleCode());
        }
        if(Objects.nonNull(roleVo.getStatus())){
            wrapper.eq("status",roleVo.getStatus());
        }
        if(Objects.nonNull(roleVo.getDateRange())&&roleVo.getDateRange().length==2){
            wrapper.between("create_time",roleVo.getDateRange()[0],roleVo.getDateRange()[1]);
        }
        IPage<Role> page1 = roleDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询角色",page1);
    }

    @Override
    public boolean delById(Long id) {
        int i = roleDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean saveAndUpdate(Role role) {
        int i=-1;
        if (role.getCreateTime()==null){
            role.setCreateTime(new Date());
            role.setUpdateTime(new Date());
        }else {
            role.setUpdateTime(new Date());
        }
        if(role.getRoleId()==null){
            i=roleDao.insert(role);
            System.out.println("====="+role.getStatus());
        }else {
            i=roleDao.updateById(role);
        }
        if (i>0){
            return true;
        }
        return false;
    }


}
